suyumen
目前主要在学习web相关

HFCTF-2021-Final-easyflask

2021-05-14 反序列化
Word count: 527 | Reading time: 2min

虽然是五一布置的作业,然而五一的时候完全不会。才对flask有一点点了解,过来写写这个试试吧,希望可以多做一些步骤。


根据页面指示,在file?file=/app/source得到源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3.6
from flask import Flask, request, render_template, session
from base64 import b64decode
import pickle
import os

app = Flask(__name__)
app.config["SECRET_KEY"] = "*******"
User = type(
'User', (object,),
{
'uname': 'test',
'is_admin': 0,
'__repr__': lambda
o: o.uname,
}
)


@ app.route('/', methods=('GET',))
def index_handler():
if not session.get('u'):
u = pickle.dumps(User())
session['u'] = u
return "/file?file=index.js"


@ app.route('/file', methods=('GET',))
def file_handler():
path = request.args.get('file')
path = os.path.join('static', path)
if not os.path.exists(path) or os.path.isdir(path) or '.py' in path or '.sh' in path or '..' in path or "flag" in path:
return 'disallowed'

with open(path, 'r') as fp:
content = fp.read()
return content


@ app.route('/admin', methods=('GET',))
def admin_handler():
try:
u = session.get('u')
if isinstance(u, dict):
u = b64decode(u.get('b'))
u = pickle.loads(u)
except Exception:
return 'uhh?'
if u.is_admin == 1:
return 'welcome, admin'
else:
return 'who are you?'


if __name__ == '__main__':
app.run('0.0.0.0', port=80, debug=False)

代码看不太懂啊,查一查函数:

isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。但type()不考虑继承关系。

其中file参数过滤了flag,.sh,..,.py,直接包含/proc/self/environ居然成功了,看一下系统变量得到secret-key

1
secret_key=glzjin22948575858jfjfjufirijidjitg3uiiuuh

看到调用了pickle.losds()函数->反序列化。

Python Pickle反序列化

就是dump函数进行序列化操作,load函数进行反序列化操作,反序列化顺序和序列化顺序一致。

此题中应该是变量覆盖一下,把is_admin的值修改为1然后获取flag路径包含一下。

还是不会,我还是看看别的题。


[参考]

https://blog.csdn.net/qq_43431158/article/details/108919605

https://blog.csdn.net/SopRomeo/article/details/116273775

https://www.jianshu.com/p/a3e8a340a6db

https://www.cnblogs.com/vstar-o/p/13411971.html

https://www.cnblogs.com/wh4am1/p/12071804.html

https://xz.aliyun.com/t/7436#toc-9这篇好详细!!

Author: suyumen

Link: https://suyumen.github.io/2021/05/14/2021-05-14-[HFCTF%202021%20Final]easyflask/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
SUCTF-2019-CheckIn
NextPost >
FBCTF2019-Event
CATALOG
  1. 1. [参考]